package com.lgd.tools;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

/**
 * Created by 299174105 on 2017/7/14.
 * GBK转换UTF-8
 */
public class Encoding {

    public static void transform(String srcDirPath, String utf8DirPath) throws IOException {
        //获取所有java文件/jsp文件等
        Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true);

        for (File javaGbkFile : javaGbkFileCol) {
            //UTF8格式文件路径
            String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
            //使用GBK读取数据，然后用UTF-8写入数据
            FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
        }
    }

    public static void main(String[] args) {

        try {
            //GBK编码格式源码路径
            String srcDirPath = "D:\\UTF8\\WebRoot";
            //转为UTF-8编码格式源码路径
            String utf8DirPath = "D:\\UTF8\\web";
            transform(srcDirPath, utf8DirPath);
            System.out.println("转换完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
